<template>
	<view>
		<!-- 列表框 -->
		<tabs @setSwipterData="setSwipterData" :tabList="tabItems"></tabs>
		<view v-if="messages.length > 0">
			<view v-for="(item, index) in messages" :key="index" class="list" @tap="goDetail" :data-id="item.id" :data-work="item.work"
			 :data-index="index">
				<view class="list-title">
					{{item.message}}
					<text class="message-watched">{{item.watched==0?"未查看":"已查看"}}</text>
				</view>
				<view class="list-date">时间：{{item.gmtModified}}</view>
			</view>
		</view>
		<view v-if="messages.length <= 0" class="message-empty">暂无相关消息</view>
	</view>
</template>

<script>
	// pages/ucenter/message/message.js
	const app = getApp();
	import tabs from "../../../components/tabs/tabs";

	export default {
		data() {
			return {
				messages: [],
				tabItems: [
					["未查看", "", true],
					["已查看", "", false]
				],
				current: 0,
				userId: ""
			};
		},

		components: {
			tabs
		},
		props: {},
		onLoad: function(options) {
			let _this = this;

			let openId = uni.getStorageSync('openid');

			if (openId != undefined && openId != "" && openId != null) {
				uni.request({
					url: `${app.globalData.baseUrl}/user/selectIsUser/${openId}`,

					success(res) {
						let userInfo = res.data.dataMap.data;

						_this.setData({
							userId: userInfo.id
						});

						_this.getUnWatched();
					}

				});
			} else {
				uni.navigateBack({
					complete: res => {
						uni.showToast({
							title: '请登录',
							icon: "none"
						});
					}
				});
			}
		},
		onPullDownRefresh: function() {
			this.getUnWatched(this.current);
			uni.stopPullDownRefresh();
		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			goDetail: function(e) {
				let id = e.currentTarget.dataset.id;
				let work = e.currentTarget.dataset.work;
				let index = e.currentTarget.dataset.index;

				let _this = this;

				let list = ['删除', '标记为已查看', '查看详情'];

				if (this.messages[index].message == "签到成功") {
					list = ['删除', '标记为已查看'];
				}

				uni.showActionSheet({
					itemList: list,
					success: res => {
						let tapIndex = res.tapIndex;

						if (tapIndex == 0) {
							uni.showModal({
								title: '删除提醒',
								content: '确定删除吗',
								success: result => {
									if (result.confirm) {
										uni.request({
											url: `${app.globalData.baseUrl}/message/delete/${id}`,
											method: "DELETE",
											success: res => {
												uni.showToast({
													title: '删除成功',
													success(res1) {
														uni.startPullDownRefresh();
													}
												});
											}
										});
									}
								}
							});
						}

						if (tapIndex == 1) {
							let form = this.messages[index];

							if (form.watched === 0) {
								uni.showLoading({
									title: '修改中'
								});
								form.watched = 1;
								uni.request({
									url: `${app.globalData.baseUrl}/message/watched`,
									complete: res => {
										uni.hideLoading();
									},
									data: form,
									fail: res => {},
									method: "PUT",
									success: result => {
										uni.startPullDownRefresh();
									}
								});
							} else {
								uni.showToast({
									title: '不可重复修改',
									icon: "none"
								});
							}

						}

						if (tapIndex == 2) {
							uni.navigateTo({
								url: `/pages/common/detail/detail?id=${work}` // 指定页面的url
							});
						}

					}
				});
			},

			setSwipterData(e) {
				const current = e.detail;
				this.setData({
					current
				});
				let _this = this;

				if (current == 0) {
					_this.getUnWatched();
				}

				if (current == 1) {
					_this.getUnWatched(1);
				}
			},

			changeStatus() {},

			getUnWatched(index = 0) {
				let _this = this;

				uni.showLoading({
					title: '加载中'
				});
				uni.request({
					url: `${app.globalData.baseUrl}/message/list/${_this.userId}?watched=${index}`,
					complete: res => {
						uni.hideLoading();
					},
					fail: res => {},
					success: result => {
						_this.setData({
							messages: result.data.dataMap.data
						});
					}
				});
			},
		}
	};
</script>
<style>
	/* pages/ucenter/message/message.wxss */
	.message-empty {
		padding-top: 5%;
		display: flex;
		justify-content: center;
	}

	.message-watched {
		color: red;
		float: right;
		font-size: smaller;
	}

	.list {
		width: 95%;
		height: 200rpx;
		margin: 2.5%;
		border-bottom: solid 1px #e6e6ee;
		background: #fff;
		padding: 5rpx 10rpx;
	}

	.list-title {
		font-size: 40rpx;
		margin-top: 20rpx;
	}

	.list-date {
		margin-top: 10rpx;
	}

	.list-claim {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
	}

	page {
		background: #efeff5;
	}
</style>
